<?php
/**
 * vim: et ts=4 sts=4 sw=4 fdm=marker
 *
 * 表字段描述生成缓存 
 *
 * @author	wang Penghai <china_php@hotmail.com>
 * @copyright 2008 5e6d Corporation.
 * @version   $Id: config.php 29 2009-03-10 14:30:18Z wph $
 */


class get_fields_disc_tool extends pf_controller {
	
	public function __construct()
	{
		parent::__construct();
		
	}

	public function index()
	{
		echo "/n";
	}

	public function create_fields()
	{
		$table_obj = new fields_show();
		$tables = $table_obj->get_tables();
		$fields_info = array();
		foreach ($tables as $val)
		{
			foreach ($val as $table)
			{
				$disc = $table_obj->get_tables_fields($table);
				foreach ($disc as $f)
				{
					$field = $f['Field'];
					$type_arr = explode('(', $f['Type']);
					$type = $type_arr[0];
					$min = 0;
					$max = isset($type_arr[1])?trim($type_arr[1], ')'):2000;
					$fields_info[$table][$field] = array (
						'type' => $type,
						'min' => $min,
						'max' => $max,
						'msg' => $f['Comment'],
						'callback'=>'',
						);
				}
			}
		}

		$msg = var_export($fields_info, true);
		file_put_contents('dd.php', $msg);
	}
}

class fields_show extends pf_model {
	public function get_tables_fields($table)
	{
		//$table = $this->tname($table);
		$sql = "SHOW FULL FIELDS FROM $table";
		$res = $this->getAll($sql);
		if (false === $res)
		{
			var_export($this->getError());
		}
		return $res;
	}

	public function show_create_table($table)
	{
		$sql = "SHOW CREATE TABLE $table";
		$res = $this->getAll($sql);
		if (false === $res)
		{
			var_export($this->getError($table));
		}
		return $res;
	}
	
	public function get_tables()
	{
		$sql = "show tables;";
		$res = $this->getAll($sql);
		if (false === $res)
		{
			var_export($this->getError());
			die('db wrong!'); 
		}
		return $res;
	}
}
